//
//  YXJCommentCell.m
//  Piece
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "YXJCommentCell.h"
#import "UIImageView+WebCache.h"

@implementation YXJCommentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.userImageView = [[UIImageView alloc] init];
        self.userImageView.layer.cornerRadius = 20;
        self.userImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_userImageView];
        
        self.nickName = [[UILabel alloc] init];
        self.nickName.textColor = [UIColor colorWithRed:0.54 green:0.74 blue:0.89 alpha:1.00];
        self.nickName.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_nickName];
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.textColor = [UIColor colorWithWhite:0.098 alpha:1.000];
        self.contentLabel.font = [UIFont systemFontOfSize:15];
        self.contentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contentLabel];
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.likeButton setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
        self.isLike = NO;
        [self.likeButton addTarget:self action:@selector(didLikeButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_likeButton];
        
        self.numerLabel = [[UILabel alloc] init];
        self.numerLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
        self.numerLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_numerLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
        self.dateLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_dateLabel];
    }
    return self;
}

- (void)didLikeButton:(UIButton *)sender
{
    if (self.isLike == NO) {
        [sender setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateNormal];
        self.numerLabel.text = [NSString stringWithFormat:@"%ld", self.contentModel.praisenum + 1];
        self.isLike = YES;
    }else
    {
        [sender setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
        self.numerLabel.text = [NSString stringWithFormat:@"%ld", self.contentModel.praisenum];
        self.isLike = NO;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.userImageView.frame = CGRectMake(10, 10, 40, 40);
    self.nickName.frame = CGRectMake(self.userImageView.frame.origin.x + self.userImageView.frame.size.width + 15, self.userImageView.frame.origin.y + 5, 100, 20);
    
    //文字的cell的高度:
    NSString *str = self.contentModel.content;
    NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.contentLabel.frame = CGRectMake(self.nickName.frame.origin.x, self.nickName.frame.origin.y + self.nickName.frame.size.height + 10, ScreenWidth - 80, newRect.size.height + 40);
    
    self.dateLabel.frame = CGRectMake(ScreenWidth - 100, self.userImageView.frame.origin.y, 100, 20);
    self.numerLabel.frame = CGRectMake(ScreenWidth - 50, self.dateLabel.frame.origin.y + self.dateLabel.frame.size.height, 50, 20);
    
    self.likeButton.frame = CGRectMake(self.numerLabel.frame.origin.x - 30, self.numerLabel.frame.origin.y - 5, 32, 32);
}

-(void)setContentModel:(YXJContentModel *)contentModel
{
    if (_contentModel != contentModel) {
        _contentModel = contentModel;
    }
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:contentModel.userModel.web_url]];
    self.nickName.text = contentModel.userModel.user_name;
    self.contentLabel.text = contentModel.content;
    self.dateLabel.text = [self changeFormString:contentModel.input_date];
    NSString *praisenumString = [NSString stringWithFormat:@"%ld", contentModel.praisenum];
    self.numerLabel.text = praisenumString;
}

- (NSString *)changeFormString:(NSString *)string
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yy-MM-dd HH:mm:ss"];//yyyyMMddHHmmss
    
    NSDate *inputDate = [inputFormatter dateFromString:string];
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init] ;
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"yyyy.MM.dd"];
    
    NSString *outputDate = [outputFormatter stringFromDate:inputDate];
    
    return outputDate;
}





@end
